<?php
class orders extends CI_Controller 
{
		
    function __construct()
	{
		parent::__construct();	
		$this->load->model('order_model');
		$this->load->model('member_model');
		$this->load->model('order_product_model');
		
		$this->load->model('category_model');
		$this->load->language('order','english');
	}
	function getPath($action = '')
	{
		return array('controller' => strtolower(__CLASS__), 'action' => strtolower($action));
	}
	function index($page = 0)
	{
		$per_page_count 	     = $this->config->item('per_page_count');
		$view 				     = $this->getPath(__FUNCTION__);
		$search                  = $this->input->post('search');
		$where                    = array('deleted' => 0);
		
		if(!empty($search))
		{
			$status                = $this->input->post('status');         // filter subject with member 
			$view['filter']        = $status ;  
			$this->session->set_userdata(array('filter' => $status));    // set filter value
			$date                  = $this->input->post('date');         // filter subject with member 
			$view['filter1']       = $date ;  
			$this->session->set_userdata(array('filter1' => $date)); 
			$address               = $this->input->post('address');         // filter subject with member 
			$view['filter2']       = $address ;  
			$this->session->set_userdata(array('filter2' => $address)); 
			
		}
		else
		{
			$view['filter']     = $this->session->userdata('filter');
			$view['filter1']      = $this->session->userdata('filter1');
			$view['filter2']      = $this->session->userdata('filter2');
		}
		if(!empty($view['filter']))
		{
		$where['status']             = $view['filter'];   //$member;
		
		}
		if(!empty($view['filter1']))
		{
			$where['created']      = $view['filter1']; 
		}
		if(!empty($view['filter2']))
		{
			$like['address']      = $view['filter2']; 
		}
		
	
		$orders                   = $this->order_model->getorders($page, $per_page_count , $like, $where,'' );
		$view['orders'] 		  = $orders->records;
		$view['cur_page'] 	      = $page;
		$view['title']		      = 'Orders Listing';
		$view['subtitle']	      = 'Orders Lists';
		/************************ Pagination block ***********************************/
		$config['base_url'] 	  = $this->config->item('admin_base_url').'/orders/index/';
		$config['total_rows'] 	  = $orders->total;
		$config['per_page']       = $per_page_count; //echo  $view['cur_page'];
		$config['cur_page']       = $page /$per_page_count;  //echo $config['cur_page'] ;
		$config['uri_segment'] 	  = 4;
		$config['num_links'] 	  = 3;
		$this->pagination->initialize($config);
		$view['msg'] 	          = $this->lang->line($this->session->flashdata('orders_msg'));
		
		$where                    = array('deleted' => 0,'parent_id' => '0');
		$category                 = $this->category_model->getcategories(0,'','', $where );
		$view['category'] 		  = $category->records;
		
		$this->layout->adminview('order/orders_view', $view);
	
	}
	function add($cur_page = 0)
	{
		$view                    = $this->getPath(__FUNCTION__);
		$view['title']           = "Orders Details";
		$save                    = $this->input->post('save');
		$view['orders'] 	     = array();
		$view['cur_page']        = $cur_page;
		$view['subtitle']	     = 'Add orders';
		
		if(!empty($save)) 
		{
			$file                    = $_FILES["picture"]["name"];
			$ext                     = end(explode('.', $file));
			
			$data                 = $this->retrieve->post($this->orders_model->fields);
			$data['image']        = $ext;
			$insertStatus         = $this->orders_model->add($data);
			$view['orders'] 	  = $data;
			
			//print_r($insertStatus);
			/****** image upload ********************************/
			
			//$up_path 				 = $this->config->item('image_path')."orderss/";
			if(!empty($ext))
			{
			$file_name               = $insertStatus['id'].".".$ext;
    		move_uploaded_file($_FILES["picture"]["tmp_name"],"images/orderss/" . 	$file_name);
			}
			/****************************************************/
			switch($insertStatus['status'])
			{
				case 1:
					$this->session->set_flashdata('orders_msg', 'orders_add_success');
					if(!empty($cur_page)) 
						redirect($this->config->item('admin_base_url').'orders/index/'.$cur_page,'location');
					else
						redirect($this->config->item('admin_base_url').'orders/index/','location');
					exit;		
					break;
				case -1:
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('orders_valid_failed');
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('orders_already_exists');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('orders_add_failure');
	
					break;
				
				default:
					$view['msg'] 	= $this->lang->line('orders_add_failure');
					break;
			}
		} 
		$where                    = array('parent_id'=>0,'deleted' => 0);
		$category                 = $this->category_model->getcategories(0,'','', $where );
		$view['category'] 		  = $category->records;
		$this->layout->adminview('orders/orders_addedit', $view);
	}

	function edit($id = '', $cur_page='')
	{
		$view                 = $this->getPath(__FUNCTION__);
		if(empty($id)) 
		{
			redirect($this->config->item('admin_base').'orders/index/'.$cur_page,'location');
			exit;
		} 
		else 
		{
			$records = $this->orders_model->getorderss(0,1,'', array('id' => $id),'');
			$orders = $records->records[0];
			if(empty($orders)) 
			{
				$this->session->set_flashdata('orders_msg', 'orders_invalid_record');
				redirect($this->config->item('admin_base').'orders/index','location');
				exit;
			}
			$view['id']		                = $id;
			
			$view['orders']	                = $orders;
		}
		
		$view['title']                 = "Edit orders ";
		$view['page_title']            = "Edit orders";
		$view['subtitle']	           = 'Edit orders';
		$save                          = $this->input->post('save');
		if(!empty($save)) 
		{
			$file                    = $_FILES["picture"]["name"];
			$ext                     = end(explode('.', $file));
			
			$data                      = $this->retrieve->post($this->orders_model->fields);
			if(!empty($ext))
			{
			$data['image']             = $ext;
			}
			$insertStatus              = $this->orders_model->udpate($id, $data);
			
			/************************ image upload **************/
			if(!empty($ext))
			{
			$file_name               = $id.".".$ext;
    		move_uploaded_file($_FILES["picture"]["tmp_name"],"images/orderss/" . 	$file_name);
			}
			switch($insertStatus)
			{
				case 1:
					$this->session->set_flashdata('orders_msg', 'orders_edit_success');
					redirect($this->config->item('admin_base_url').'orders/index/'.$cur_page,'location');
					exit;		
					break;
				case -1:
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('orders_already_exists');
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('orders_valid_failed');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('orders_edit_failure');
					break;
				;
				default:
					$view['msg'] 	= $this->lang->line('orders_edit_failure');
					break;
			}
			$view['orders'] 	       = $data;
		}
		$view['cur_page']              = $cur_page; 
		$where                    = array('deleted' => 0,'parent_id'=>0);
		$category                   = $this->category_model->getcategories(0,'','', $where );
		$view['category'] 		  = $category->records;
		$subcategory              = $this->category_model->getcategories(0,'','', array('id'=>$view['orders']['category'],'deleted' => 0) );
		$subcategory              = $subcategory->records[0];
		$view['p_id']             = $subcategory['parent_id'];
		$subcategory                   = $this->category_model->getcategories(0,'','', array('parent_id'=>$subcategory['parent_id'],'deleted' => 0) );
		$view['subcategory'] 		  = $subcategory->records;
		$this->layout->adminview('orders/orders_addedit', $view);
	}
	function view($id = '',$cur_page='')
	{
		
		$orders               = $this->order_product_model->getorder_products(0,'',$like, array('order_products.order_id'=>$id),'');
		$view['orders']       = $orders->records;
		//print_r($view['orders']);
		$view['image_path']   = $this->config->item('image_path');
		
		$this->load->view('admin/order/order_info', $view);
	}
	function success($id = '')
	{
		
			$data['status']            = "delivered";
			$insertStatus              = $this->order_model->udpate($id, $data);
			echo $insertStatus ;
			switch($insertStatus)
			{
				case 1:
					$this->session->set_flashdata('orders_msg', 'orders_delivered_success');
					redirect($this->config->item('admin_base_url').'orders/index/'.$cur_page,'location');
					exit;		
					break;
				case -1:
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('orders_already_exists');
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('orders_valid_failed');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('orders_edit_failure');
					break;
				;
				default:
					$view['msg'] 	= $this->lang->line('orders_edit_failure');
					break;
			}
			
	}
	function cancell($id = '')
	{
			
			$data['status']            = "cancelled";
			$insertStatus              = $this->order_model->udpate($id, $data);
			switch($insertStatus)
			{
				case 1:
					$this->session->set_flashdata('orders_msg', 'orders_cancel_success');
					redirect($this->config->item('admin_base_url').'orders/index/'.$cur_page,'location');
					exit;		
					break;
				case -1:
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('orders_already_exists');
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('orders_valid_failed');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('orders_edit_failure');
					break;
				;
				default:
					$view['msg'] 	= $this->lang->line('orders_edit_failure');
					break;
			}
	}
	
	function cancel()
	{
    redirect($this->config->item('admin_base_url').'orders/index','location');
		exit;	
	}
	
	function delete($id = '', $cur_page)
	{
		if(empty($id)) 
		{
			redirect($this->config->item('admin_base_url').'orders/index/'.$cur_page,'location');
			exit;
		} 
		else 
		{
			$deleteStatus = $this->orders_model->delete($id);
			switch($deleteStatus)
			{
				case 1:
					$this->session->set_flashdata('users_msg', 'orders_delete_success');
					redirect($this->config->item('admin_base_url').'orders/index/'.$cur_page,'location');
					exit;		
					break;
				case false:
					$view['msg'] 	    = $this->lang->line('orders_delete_failure');
					break;
			}
		}
		
	}
	function clearFilter()
	{
		$this->session->unset_userdata('filter');
		$this->session->unset_userdata('filter1');
		$this->session->unset_userdata('filter2');
		$this->session->unset_userdata('filter3');
		redirect($this->config->item('admin_base_url').'orders/index/','location');
	}
}
?>